package com.handong.handserialprogram.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handong.handserialprogram.R;

import java.lang.reflect.Field;

import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

/**
 * Created by userKey on 2017/4/26.
 */

public class ViewUtil {

    public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; //需要自己定义标志

    public static void removeHomeKeyListener(Dialog dialog) {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int keycode, KeyEvent event) {
                if ((keycode == KeyEvent.KEYCODE_HOME || keycode == KeyEvent.KEYCODE_VOLUME_UP)) {
                    return true;
                }
                return false;
            }
        });
        dialog.show();
        //ialog.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);
    }

    public static void removeAllKeyListener(Dialog dialog) {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialogInterface, int keycode, KeyEvent event) {
                if ((keycode == KeyEvent.KEYCODE_HOME) || (keycode == KeyEvent.KEYCODE_BACK)||(keycode == KeyEvent.KEYCODE_MENU)) {
                    return true;
                }
                return false;
            }
        });
        dialog.show();
        //dialog.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);
    }

    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager      只要是个manager  对象就可以，重新new一个也可以
     * @param recyclerView 当前的RecyclerView
     * @param n            要跳转的位置
     */
    public static void moveToPosition(LinearLayoutManager manager, RecyclerView recyclerView, int n) {

        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            recyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = recyclerView.getChildAt(n - firstItem).getTop();
            recyclerView.scrollBy(0, top);
        } else {
            recyclerView.scrollToPosition(n);
        }
    }

    /**
     * RecyclerView打开默认局部刷新动画
     */
    public static void openDefaultAnimator(RecyclerView recyclerView) {
        recyclerView.getItemAnimator().setAddDuration(120);
        recyclerView.getItemAnimator().setChangeDuration(250);
        recyclerView.getItemAnimator().setMoveDuration(250);
        recyclerView.getItemAnimator().setRemoveDuration(120);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(true);
    }

    /**
     * RecyclerView关闭默认局部刷新动画
     */
    public static void closeDefaultAnimator(RecyclerView recyclerView) {
        recyclerView.getItemAnimator().setAddDuration(0);
        recyclerView.getItemAnimator().setChangeDuration(0);
        recyclerView.getItemAnimator().setMoveDuration(0);
        recyclerView.getItemAnimator().setRemoveDuration(0);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    /**
     * 动态生成Loading布局。
     *
     * @param context 上下文对象。
     * @param content 加载内容。
     * @return LinearLayout对象。
     */
    private LinearLayout getLoadingView(Context context, String content) {
        LinearLayout layout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(params);
        layout.setPadding(8, 8, 8, 8);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER_VERTICAL);

        MaterialProgressBar progressBar = new MaterialProgressBar(context);
        LinearLayout.LayoutParams paramsBar = new LinearLayout.LayoutParams(52, 52);
        progressBar.setLayoutParams(paramsBar);
        progressBar.setIndeterminateTintList(AppCompatResources.getColorStateList(context.getApplicationContext(),
                R.color.theme_white_text_content));
        layout.addView(progressBar);

        TextView contentText = new TextView(context);
        LinearLayout.LayoutParams paramsText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsText.leftMargin = 23;
        contentText.setLayoutParams(paramsText);
        contentText.setText(content);
        contentText.setTextSize(15f);
        contentText.setTextColor(context.getApplicationContext().getResources().getColor(R.color.theme_white_text_content));
        layout.addView(contentText);

        return layout;
    }

    /**
     * 修改提示竖线。
     *
     * @param editText
     */
    public static void changeEditTextStatus(EditText editText) {
        try {
            Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
            field.setAccessible(true);
            field.set(editText, R.drawable.edittext_cursor_drawable);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static LinearLayout showTipLayout(Context context, int imageId, int textId, View.OnClickListener listener) {
            LinearLayout layout = (LinearLayout) LayoutInflater.from(context.getApplicationContext())
                    .inflate(R.layout.load_result_tip, null);
            ImageView imageView = (ImageView) layout.getChildAt(0);
            imageView.setImageResource(imageId);
            TextView textView = (TextView) layout.getChildAt(1);
            textView.setText(textId);
            if (null != listener) {
                layout.setOnClickListener(listener);
            }
        return layout;
    }

    /**
     * 改变按钮显示
     * @param view
     * @param color
     * @param resId
     */
    public static void changeBtnStatus(TextView view, int color, int resId) {
        GradientDrawable drawable = (GradientDrawable) view.getBackground();
        drawable.setColor(color);
        view.setText(resId);
    }
}
